﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.WindowsAPICodePack.Sensors;

namespace DemoApp
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SensorManager.SensorsChanged += SensorManagerSensorsChanged;
            UpdateSensorsList();
        }

        void SensorManagerSensorsChanged(SensorsChangedEventArgs change)
        {
            Dispatcher.Invoke((System.Threading.ThreadStart)(UpdateSensorsList));
        }

        private void UpdateSensorsList()
        {
            foreach (var sensor in SensorManager.GetSensorsByTypeId<Accelerometer3D>())
            {
                sensor.DataReportChanged +=
                    delegate(Sensor sender, EventArgs e)
                    {
                        Dispatcher.Invoke((System.Threading.ThreadStart)(
                            delegate
                            {
                                if (UseXCoordinate.IsChecked == true)
                                    CurrentXValue.Text = ((Accelerometer3D)sender).CurrentAcceleration[AccelerationAxis.X].ToString();
                                if (UseYCoordinate.IsChecked == true)
                                    CurrentYValue.Text = ((Accelerometer3D)sender).CurrentAcceleration[AccelerationAxis.Y].ToString();
                                if (UseZCoordinate.IsChecked == true)
                                    CurrentZValue.Text = ((Accelerometer3D)sender).CurrentAcceleration[AccelerationAxis.Z].ToString();
                            }));
                    };
            }

            ActiveSensors1ListBox.ItemsSource = SensorManager.GetSensorsByTypeId<Accelerometer3D>().ToArray();
            ActiveSensors1ListBox.SelectedIndex = 0;
        }
    }

    class CoordinateConverter : IValueConverter
    {
        public double Sensitive { get; set; }

        #region IValueConverter Members

        private double previousValue = 0;

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double result = 0;
            double.TryParse(value.ToString(), out result);
            return previousValue = ((result * Sensitive + previousValue) / 3);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }

    class LightConverter : IValueConverter
    {
        public double Sensitivity
        {
            get;
            set;
        }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double currentValue = 0;

            if (value is double)
            {
                currentValue = (double)value;
            }
            else
            {
                double.TryParse(value.ToString(), out currentValue);
            }

            byte result = (byte)(currentValue * 2 + 30 < 255 ? Math.Round(currentValue * 2 + 30) : 255);

            return Color.FromRgb(result, result, result);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }

    class AmbientCorrectionConverter : DependencyObject, IValueConverter
    {
        public AmbientCorrectionConverter()
        {
            Sensitivity = 100;
        }

        public double Sensitivity
        {
            get;
            set;
        }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double currentValue = 0;

            if (value is double)
            {
                currentValue = (double)value;
            }
            else
            {
                double.TryParse(value.ToString(), out currentValue);
            }

            return (currentValue / Sensitivity < 1 ? currentValue / Sensitivity : 1) - 0.4;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        #endregion
    }


}